package com.skycong.hello77.activity.contentmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.skycong.hello77.R;

/**
 * @author ruanmingcong
 * @date 2018/1/13 16:04
 */
public class ContentMenuActivity extends Activity{
    private ListView listMenu;
    private ArrayAdapter<String> arr_adapter;//数组适配器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_menu);

        listMenu = (ListView) findViewById(R.id.list_view_menu);
         String data[]= {"菜单一","菜单二","菜单三","菜单四","菜单五"};
        arr_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
        listMenu.setAdapter(arr_adapter);
        //注册上下文菜单,设置上下文内容
        this.registerForContextMenu(listMenu);


    }

   /**
    * 添加上下文内容
    * */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderIcon(R.mipmap.icon_touxiang);
        menu.setHeaderTitle("菜单操作");
        menu.add(1,100,1,"添加");
        menu.add(1,101,1,"删除");
        menu.add(1,102,1,"复制");
        menu.add(1,103,1,"剪切");
        menu.add(1,104,1,"查看");

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 100:{
                Toast.makeText(ContentMenuActivity.this, "点击了复制", Toast.LENGTH_SHORT).show();
                break;
            }
            case 101:{
                Toast.makeText(ContentMenuActivity.this, "删除", Toast.LENGTH_SHORT).show();
                break;
            }
            case 102:{
                Toast.makeText(ContentMenuActivity.this, "复制", Toast.LENGTH_SHORT).show();
                break;
            }
            case 103:{
                Toast.makeText(ContentMenuActivity.this, "剪切", Toast.LENGTH_SHORT).show();
                break;
            }
            case 104:{
                Toast.makeText(ContentMenuActivity.this, "查看", Toast.LENGTH_SHORT).show();
                break;
            }
        }
        return super.onContextItemSelected(item);
    }
}
